﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.WebControls;
using System.Collections;
using Microsoft.SharePoint;

namespace LookupFieldEntityPicker.CustomLookup
{
    public class RecordLookupFieldControl : BaseFieldControl
    {
        private RecordPicker _recordPicker = null;

        /// <summary>
        /// custom property 
        /// </summary>
        public string SearchableFields
        {
            get
            {
                var field = Field as DEMOLookupPicker;
                if (field != null)
                {
                    return field.SearchableFields;
                }
                return string.Empty;
            }
        }

        public RecordLookupFieldControl()
        {
        }

        public override void Validate()
        {
            base.Validate();
            if (_recordPicker != null)
            {
                if (base.Field.Required)
                    IsValid = _recordPicker.IsValid;
                else
                    IsValid = true;
            }
               
        }

        protected override void CreateChildControls()
        {
            if (Field == null)
            {
                return;
            }
            base.CreateChildControls();

            if (base.ControlMode == SPControlMode.Edit || base.ControlMode == SPControlMode.New)
            {
                
                _recordPicker = new RecordPicker() { FieldSeachColumns = SearchableFields };
                this.Controls.Add(_recordPicker);
            }
            base.ChildControlsCreated = true;
        }


        public override object Value
        {
            get
            {
                this.EnsureChildControls();

                SPFieldLookupValue val = null;
                if (this._recordPicker != null)
                {
                    if (this._recordPicker.ResolvedEntities.Count > 0)
                    {
                        PickerEntity pickerEntity = (PickerEntity)this._recordPicker.ResolvedEntities[0];
                        val = new SPFieldLookupValue(Int32.Parse(pickerEntity.Key), pickerEntity.EntityData[pickerEntity.Key].ToString());
                    }

                    if (val == null)
                        _recordPicker.IsValid = false;
                }

                return val;
            }
            set
            {
                this.EnsureChildControls();
                DEMOLookupPicker recordLookupField = (DEMOLookupPicker)base.Field;

                SPFieldLookupValue lookupValue = (recordLookupField.FieldRenderingControl as RecordLookupFieldControl).ItemFieldValue as SPFieldLookupValue;
               
               
                PickerEntity pickerEntity = new PickerEntity
                {
                    Key = lookupValue.LookupId.ToString(),
                    IsResolved = true,
                    DisplayText = lookupValue.LookupValue,
                    Description = "" //TODO: you can execute SPQuery and build up the description here
                };
                pickerEntity.EntityData.Add(lookupValue.LookupId.ToString(), lookupValue.LookupValue);
                ArrayList arrayList = new ArrayList();
                arrayList.Add(pickerEntity);
                this._recordPicker.UpdateEntities(arrayList);
            }
        }

       
    }
}
